#' ---
#' title: "Text as Behavior - Replication Archive Build Script"
#' author: "Omar Wasow"
#' date: "`r Sys.Date()`"
#' description: |
#'   Standalone replication script for "Text as Behavior" paper.
#' ---
#'
#' =============================================================================
#' USAGE
#' =============================================================================
#'
#' This script supports two replication modes:
#'
#' 1. FROM PRE-PROCESSED DATA (default):
#'    Uses existing .Rdata files in text_data_output/ to render the PDF.
#'    This is faster and does not require downloading raw data.
#'
#'      Rscript text_code/make_script_rep.R
#'
#' 2. FROM RAW DATA (full replication):
#'    Processes raw survey data from text_data_raw/ before rendering.
#'    Requires downloading ANES, Afrobarometer, and KMM datasets first.
#'    See text_data_raw/Read_Me_Text_Data_Raw.txt for download instructions.
#'
#'      Rscript text_code/make_script_rep.R --from-raw
#'
#' =============================================================================

# =============================================================================
# Parse Command-Line Arguments
# =============================================================================

args <- commandArgs(trailingOnly = TRUE)
from_raw <- "--from-raw" %in% args

if (from_raw) {
    message("=== Running FULL replication from raw data ===")
} else {
    message("=== Running replication from pre-processed data ===")
    message("    (Use --from-raw to process from raw data)")
}

# =============================================================================
# Load Packages and Functions (replication versions)
# =============================================================================

source(here::here("text_code", "install_packages.R"))
source(here::here("text_code", "text_packages_rep.R"))
source(here::here("text_code", "text_functions_rep.R"))
source(here::here("text_code", "custom_ggplot_themes.R"))
source(here::here("text_code", "custom_table_functions_rep.R"))

# =============================================================================
# Process Data (only if --from-raw)
# =============================================================================

if (from_raw) {

    message("\nProcessing raw data...")

    # ANES 2016: Process raw survey data and open-ended responses
    # Output: text_data_output/anes2016_processed.Rdata
    source(here::here("text_code", "anes2016_processing_rep.R"))

    # ANES 2020: Merge with 2016 panel, add validated vote and text measures
    # Requires: anes2016_processed.Rdata
    # Output: text_data_output/anes2020_merged.Rdata
    source(here::here("text_code", "anes2020_processing_rep.R"))

    # ANES 2024: Merge with 2016-2020 panel, add 2024 text measures
    # Requires: anes2016_processed.Rdata, anes2020_merged.Rdata
    # Output: text_data_output/anes2024_merged.Rdata
    source(here::here("text_code", "anes2024_processing_rep.R"))

    # Afrobarometer: Process democracy open-ended responses across 36 countries
    # Output: text_data_output/afrobarometer_processed.Rdata
    source(here::here("text_code", "afro_processing_rep.R"))

    # KMM Experiment: Process social exclusion experiment data
    # Output: text_data_output/aap_processed.Rdata
    source(here::here("text_code", "aap_processing_rep.R"))

    # =========================================================================
    # Generate Codebooks
    # =========================================================================

    # Create PDF codebooks documenting variables used in analyses
    # Output: text_data_output/codebook_anes.pdf, codebook_afrobarometer.pdf, codebook_kmm.pdf
    source(here::here("text_code", "create_codebooks.R"))

    message("Data processing complete.\n")

} else {

    # Verify pre-processed data exists
    required_files <- c(
        "anes2016_processed.Rdata",
        "anes2020_merged.Rdata",
        "anes2024_merged.Rdata",
        "afrobarometer_processed.Rdata",
        "aap_processed.Rdata"
    )

    missing <- required_files[!file.exists(
        file.path(here::here("text_data_output"), required_files)
    )]

    if (length(missing) > 0) {
        stop(
            "Missing pre-processed data files:\n  ",
            paste(missing, collapse = "\n  "),
            "\n\nEither:\n",
            "  1. Ensure text_data_output/ contains all .Rdata files, or\n",
            "  2. Run with --from-raw to process from raw data"
        )
    }

    message("Found all pre-processed data files.\n")
}

# =============================================================================
# Render PDF
# =============================================================================

message("Rendering PDF...")

rmarkdown::render(
    input         = here::here("text_docs", "text_as_behavior_rep.Rmd"),
    output_format = "pdf_document",
    output_dir    = here::here("text_docs"),
    clean         = FALSE
)

message("\nReplication build complete!")
